import edu.csl.study.scala.basic.LiuTeacher
import edu.csl.study.scala.basic.Item
import edu.csl.study.scala.basic.demo.Person

/**
1.flatMap
2.reduce 等价于reduceLeft
3.fold与reduce很像，但是多了一个指定初始值参数
4.groupBy 分组
*/

  println("Welcome to the Scala worksheet")
  val l2 = List("tom M 23","rose F 30","jary M 40")

  //过滤出男性数据
  val r1 = l2.map{x => x.split(" ")}
  .filter{arr => arr(1).equals("M")}
  .map(arr => arr.mkString(" "))
  //只改变数据格式
  l2.map(x => x.split(" "))

  //扁平化数据，改变元素形式也改变个数
  l2.flatMap(x => x.split(" "))
  
  val l3 = List(1,2,3,4)
  //reduce规约方法
  //1. a = 1  b = 3  a+b = 3
  //2. a = 3  b = 3  a+b = 6
  //3. a = 6  b = 4  a+b = 10
  val r2 = l3.reduce{(a,b) =>{a + b}}
  val r21 = l3.reduce{_+_}
  val r3 = l3.reduce{(a,b) =>{a * b}}
  val r31 = l3.reduce{_*_}
  //fold相对与reduce，能指定一个初始值
  val r4 = l3.fold(15)((a,b) =>{a + b})
  val r41 = l3.fold(15)(_+_)
  val l4 = List("hello","hello","world")
  //groupBy
  val r5 = l4.groupBy(x => x+100)

  //----------------类---------
  val p1 = new Person()

  p1.setName("tom")
  p1.setAge ( 44)
  p1.sleep()
  val v1 = p1.getAge()
  println(s"姓名: ${p1.name2}, 性别：${p1.sex2}")
  Person.staticMethod()
  
  val t1 = new LiuTeacher()

  t1.moto()
  t1.car("red")


  //-------------------样例类------
  val i1 = Item("tom",123)
  val i2 = Item
  
  //---------option两个子类：Some和None--------
  //一般是为了避免程序出现抛出异常
  //掌握Some类型的取值方法
  def f1(a:Int,b:Int)={
	   if(b != 0){
	     Some(a/b)
	   }else{
	     None
	   }
  }
  //取值
  f1(4,0).getOrElse(1000)
          
